<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'libraries/facebook/facebook.php';
class Fbci extends CI_Controller {
   public function __construct(){
            parent::__construct();
            $this->load->library('session');  
            $this->config->load('facebook');
            	$this->load->model('news_model');
    }
    public function index()
    {
        $this->load->view('main');
    }
 
    function logout(){
      //  $base_url=$this->config->item('base_url');
        $this->session->sess_destroy();
        $this->load->view('main');
       
      //  header('Location: '.$base_url);
    }
    function fblogin(){
       
   $base_url=$this->config->item('base_url');
               
          $facebook = new Facebook(array(
          'appId'     =>  $this->config->item('appID'),
          'secret'    => $this->config->item('appSecret'),
          ));
           
          $user = $facebook->getUser(); // Get the facebook user id 
        
          if($user){
            
            try{
                $user_profile = $facebook->api('/me');  //Get the facebook user profile data
                
                $params = array('next' => $base_url.'/fbci/logout');
                
                $ses_user=array('User'=>$user_profile,
                   'logout' =>$facebook->getLogoutUrl($params)   //generating the logout url for facebook 
                );
                #insert user in mysql
                #check email exits
                    if($this->check_email($user_profile['username'])){
                        
                    }else{
                        $dataadd            =   array(
                           // "name"          =>  $user_profile['name'],
//                            "opentid"          =>  $user_profile['id'],
                            "username"      =>  $user_profile['username']
//                            "email"         =>  $user_profile['email'],
//                            "link"          =>  $user_profile['link'],
//                            "group"         =>  3,
//                            "date"          =>  strtotime(date("Y-m-d H:i:s")),
//                            "birthday"      =>  $user_profile['birthday'],
//                            "gender"        =>  $user_profile['gender'],
//                            "published"     => 1
                        );
                        $this->news_model->adduser($dataadd);
                        
                    }
                #end check email
                $this->session->set_userdata($ses_user);
                #header('Location: '.$base_url);
                
            }catch(FacebookApiException $e){
                error_log($e);
                $user = NULL;
            }    
            #redirect(base_url('fbci/adduser'));    
        }    
        $this->load->view('main');
           
          
    }
     public function check_email($str){
        $this->db->where('username',$str);
        $query  = $this->db->get('user');
        if($query->row()){
            return TRUE;
        }else{
            return FALSE;
        }
        
    }
}